package com.ruoyi.appService.testresult.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.appService.mentalTestResult.huolandResult.domain.ZTestHuolandTestresult;
import com.ruoyi.appService.wxuser.domain.ZWxuser;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.WxTokenService;
import com.ruoyi.framework.web.service.wxUserRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.appService.testresult.mapper.ZCollegeStudentsTestresultMapper;
import com.ruoyi.appService.testresult.domain.ZCollegeStudentsTestresult;
import com.ruoyi.appService.testresult.service.IZCollegeStudentsTestresultService;

import javax.servlet.http.HttpServletRequest;

/**
 * 大学生心理测试结果Service业务层处理
 *
 * @author yuqihe
 * @date 2025-05-17
 */
@Service
public class ZCollegeStudentsTestresultServiceImpl extends ServiceImpl<ZCollegeStudentsTestresultMapper, ZCollegeStudentsTestresult> implements IZCollegeStudentsTestresultService {
    @Autowired
    private ZCollegeStudentsTestresultMapper zCollegeStudentsTestresultMapper;
    @Autowired
    private WxTokenService wxTokenService;

    /**
     * 查询大学生心理测试结果
     *
     * @param id 大学生心理测试结果主键
     * @return 大学生心理测试结果
     */
    @Override
    public ZCollegeStudentsTestresult selectZCollegeStudentsTestresultById(Long id) {
        return zCollegeStudentsTestresultMapper.selectZCollegeStudentsTestresultById(id);
    }

    /**
     * 查询大学生心理测试结果列表
     *
     * @param zCollegeStudentsTestresult 大学生心理测试结果
     * @return 大学生心理测试结果
     */
    @Override
    public List<ZCollegeStudentsTestresult> selectZCollegeStudentsTestresultList(ZCollegeStudentsTestresult zCollegeStudentsTestresult) {
        return zCollegeStudentsTestresultMapper.selectZCollegeStudentsTestresultList(zCollegeStudentsTestresult);
    }

    /**
     * 新增大学生心理测试结果
     *
     * @param zCollegeStudentsTestresult 大学生心理测试结果
     * @return 结果
     */
    @Override
    public int insertZCollegeStudentsTestresult(ZCollegeStudentsTestresult zCollegeStudentsTestresult) {
            return zCollegeStudentsTestresultMapper.insertZCollegeStudentsTestresult(zCollegeStudentsTestresult);
    }

    /**
     * 修改大学生心理测试结果
     *
     * @param zCollegeStudentsTestresult 大学生心理测试结果
     * @return 结果
     */
    @Override
    public int updateZCollegeStudentsTestresult(ZCollegeStudentsTestresult zCollegeStudentsTestresult) {
        return zCollegeStudentsTestresultMapper.updateZCollegeStudentsTestresult(zCollegeStudentsTestresult);
    }

    /**
     * 批量删除大学生心理测试结果
     *
     * @param ids 需要删除的大学生心理测试结果主键
     * @return 结果
     */
    @Override
    public int deleteZCollegeStudentsTestresultByIds(Long[] ids) {
        return zCollegeStudentsTestresultMapper.deleteZCollegeStudentsTestresultByIds(ids);
    }

    /**
     * 删除大学生心理测试结果信息
     *
     * @param id 大学生心理测试结果主键
     * @return 结果
     */
    @Override
    public int deleteZCollegeStudentsTestresultById(Long id) {
        return zCollegeStudentsTestresultMapper.deleteZCollegeStudentsTestresultById(id);
    }

    @Override
    public void addOrupdateTestResult(ZCollegeStudentsTestresult zCollegeStudentsTestresult, HttpServletRequest request) {

//        先获取用户id
        String token = wxTokenService.getToken(request);
        wxUserRedis wxuserRedis = wxTokenService.getwxUser(token);
        int userId=wxuserRedis.getUserId();


//根据userid判断用户是否做过测试
        ZCollegeStudentsTestresult z1=new ZCollegeStudentsTestresult();
        z1.setUserid((long)userId);
        List<ZCollegeStudentsTestresult> isExist=  zCollegeStudentsTestresultMapper.selectZCollegeStudentsTestresultList(z1);
        zCollegeStudentsTestresult.setUserid ((long) userId);

        if (isExist.size()>0) {
//            已经做过测试，执行修改逻辑
            zCollegeStudentsTestresultMapper.updateZCollegeStudentsTestresult(zCollegeStudentsTestresult);
        }else{
//            第一次做测试，执行新增逻辑
            zCollegeStudentsTestresultMapper.insertZCollegeStudentsTestresult(zCollegeStudentsTestresult);
        }
    }

    @Override
    public AjaxResult selectTestResult(HttpServletRequest request) {
        //        先获取用户id
        String token = wxTokenService.getToken(request);
        wxUserRedis wxuserRedis = wxTokenService.getwxUser(token);
        int userId=wxuserRedis.getUserId();

        //查询测试结果
        ZCollegeStudentsTestresult z1=new ZCollegeStudentsTestresult();
        z1.setUserid((long) userId);
         ZCollegeStudentsTestresult testresult= zCollegeStudentsTestresultMapper.selectZCollegeStudentsTestresultList(z1).get(0);
        if(StringUtils.isNull(testresult)){
            return AjaxResult.error("该用户没有进行测试");
        }else {
            return  AjaxResult.success(testresult);
        }
    }



}